$(function () {
    // 评论
    // 处理原始的日期对象
    let layer = layui.layer;
    template.defaults.imports.formatDate = function (data) {
        data = new Date(data)
        let year = data.getFullYear()
        let month = data.getMonth() + 1
        let day = data.getDate()
        return year + '-' + month + '-' + day
    }
    // 获取评论列表
    function loadCommmentList() {
        $.ajax({
            type: 'get',
            url: 'admin/comments',
            success: function (res) {
                // console.log(res);
                let tags = template('table-tpl', res)
                $('.layui-table tbody').html(tags)
            }
        })
    }
    loadCommmentList()

    // 删除评论
    $('.layui-table tbody').on('click', '.delete', function (e) {
        let id = $(e.target).data('id')
        layer.confirm('这么狠,你确认删除?', function (index) {
            $.ajax({
                type: 'delete',
                url: 'admin/comments/' + id,
                success: function (res) {
                    // console.log(res);
                    if (res.status === 0) {
                        layer.close(index)
                        loadCommmentList()
                    }
                }

            })
        })
    })
})